#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2024/9/16 20:54
# @Author  : herbert
# @File    : replace_file_content.py
# @Software: IntelliJ IDEA
import os


def back_file(original_file_name):
    # 新文件名
    new_file_name = original_file_name + ".bak"
    if os.path.exists(original_file_name):
        # 使用os.rename()来重命名文件
        os.rename(original_file_name, new_file_name)
        print(f"succeeded {original_file_name} backup to {new_file_name} ！")
        return True
    print(f" failed backup  {original_file_name} ,it is not exists！")
    raise Exception("异常：未备份成功")  # 如果未备份成功则程序停止


def replace_file_contents(original_file_name,old_str,new_str):
    with open(original_file_name, 'r', encoding='utf-8') as file:
        content = file.read()
        content = content.replace(old_str,new_str)
        # 将新内容写回文件
    with open(original_file_name, 'w', encoding='utf-8') as file:
        file.write(content)
    print("replace content done")

# #distributionUrl=https\://services.gradle.org/distributions/gradle-8.3-bin.zip
# distributionUrl=https\://mirrors.cloud.tencent.com/gradle/gradle-8.3-bin.zip
# gradle-wrapper.properties

def replace_gradle_wrapper(root_dir):
    for root, dirs, files in os.walk(root_dir):
        for file_name in files:
            if file_name == "gradle-wrapper.properties":
                # print(os.path.join(root, file_name))  # 文件全目录
                file_path= os.path.join(root, file_name)
                replace_file_contents(file_path,r'distributionUrl=https\://services.gradle.org/distributions',r'distributionUrl=https\://mirrors.cloud.tencent.com/gradle')
                print(f"{file_path} distributionUrl replaced")

def replace_application_db_url_and_secret(root_dir):
    for root, dirs, files in os.walk(root_dir):
        for file_name in files:
            if file_name == "application.yml":
                # print(os.path.join(root, file_name))  # 文件全目录
                file_path= os.path.join(root, file_name)
                replace_file_contents(file_path,'mango','mangodb')
                print(f"{file_path} mango->mangodb replaced")
                replace_file_contents(file_path,'123456','zxhy.123456')
                print(f"{file_path} 123456-> zxhy.123456 replaced")


def replace_gradle_repo(root_dir):
    count = 0
    for root, dirs, files in os.walk(root_dir):
        for file_name in files:
            if file_name == "build.gradle":
                # print(os.path.join(root, file_name))  # 文件全目录
                file_path = os.path.join(root, file_name)
                replace_file_contents(file_path,'mavenCentral()',"maven { url 'https://maven.aliyun.com/repository/public' }\n    mavenCentral()")
                count += 1
    print(f"file count = {count}")


if __name__ == '__main__':
    # replace_file_contents(file_path)
    #dir= "/home/herbert/IdeaProjects/spring-authorization-server-debug-record"
    # dir= "/home/herbert/IdeaProjects/gradle-test"
    replace_gradle_wrapper(os.getcwd())
    # replace_gradle_repo(dir)
    # replace_application_db_url_and_secret(dir)
